import React from 'react';
import {injectIntl} from './intl';
import PropTypes from 'prop-types';
import {Button, Spin} from 'antd';
import {hasPermissions} from './utils';
import {PERMISSION} from './constants';
import {PAGE} from '../constants';
import routes from './routes';
import WrappedPopconfirm from '../common/WrappedPopconfirm';
import styles from './style.css';
import CommonDetail from './CommonDetail';
export class Detail extends React.PureComponent {
    state = {
        loading: false,
    };

    componentDidMount() {
        this.props.init();
        this.ismounted = true;
    }

    componentWillUnmount() {
        this.ismounted = false;
        this.props.onUmount();
    }
    handleSubmitClick = () => {
        this.setState({loading: true});
        const data = {
            rowVersion: this.props.detail.rowVersion
        };
        this.props.onClickSubmit(this.state.data.id, data)
            .then(() => {
                this.setState({loading: false});
            });
    };
    handleReleaseClick = () => {
        this.setState({loading: true});
        const data = {
            rowVersion: this.props.detail.rowVersion
        };
        this.props.onClickRelease(this.state.data.id, data)
            .then(() => {
                this.setState({loading: false});
            });
    };
    handleCloseClick = () => {
        this.setState({loading: true});
        const data = {
            rowVersion: this.props.detail.rowVersion
        };
        this.props.onClickClose(this.state.data.id, data)
            .then(() => {
                this.setState({loading: false});
            });
    };
    render() {
        const {options = []} = this.props.detail;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.update) && hasPermissions(options, PERMISSION.update))
            operations.push(
                <Button key="update" href={`#${routes.update.format(this.props.id)}`} size="small">
                    {this.props.intl.formatMessage({
                        id: 'detail.btn.update',
                        defaultMessage: '编辑'
                    })}
                </Button>
            );
        if(hasPermissions(this.props.permissions, PERMISSION.submit) && hasPermissions(options, PERMISSION.submit))
            operations.push(
                <WrappedPopconfirm
                    id={this.props.id}
                    key="submit"
                    title={this.props.intl.formatMessage({
                        id: 'detail.popconfirm.message',
                        defaultMessage: '是否继续?'
                    })}
                    onConfirm={this.handleSubmitClick}>
                    <Button size="small">
                        {this.props.intl.formatMessage({
                            id: 'detail.btn.submit',
                            defaultMessage: '提交'
                        })}
                    </Button>
                </WrappedPopconfirm>
            );
        if(hasPermissions(this.props.permissions, PERMISSION.release) && hasPermissions(options, PERMISSION.release))
            operations.push(
                <WrappedPopconfirm
                    id={this.props.id}
                    key="release"
                    title={this.props.intl.formatMessage({
                        id: 'detail.popconfirm.message',
                        defaultMessage: '是否继续?'
                    })}
                    onConfirm={this.handleReleaseClick}>
                    <Button size="small">
                        {this.props.intl.formatMessage({
                            id: 'detail.btn.release',
                            defaultMessage: '发布'
                        })}
                    </Button>
                </WrappedPopconfirm>
            );
        if(hasPermissions(this.props.permissions, PERMISSION.close) && hasPermissions(options, PERMISSION.close))
            operations.push(
                <WrappedPopconfirm
                    id={this.props.id}
                    key="close"
                    title={this.props.intl.formatMessage({
                        id: 'detail.popconfirm.message',
                        defaultMessage: '是否继续?'
                    })}
                    onConfirm={this.handleCloseClick}>
                    <Button size="small">
                        {this.props.intl.formatMessage({
                            id: 'detail.btn.close',
                            defaultMessage: '关闭'
                        })}
                    </Button>
                </WrappedPopconfirm>
            );
        return (
            <div className={styles.detail}>
                <Spin spinning={this.props.loading || this.state.loading}>
                    <CommonDetail
                        loading={this.props.loading}
                        detail={this.props.detail}
                        operations={operations}/>
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" disabled={this.props.loading} onClick={this.props.onCancel}>
                        {this.props.intl.formatMessage({
                            id: 'detail.btn.retrun',
                            defaultMessage: '返回'
                        })}
                    </Button>
                </div>
            </div>
        );
    }
}
Detail.propTypes = {
    detail: PropTypes.object.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    query: PropTypes.object.isRequired,
    onCancel: PropTypes.func.isRequired,
    onClickClose: PropTypes.func.isRequired,
    onClickRelease: PropTypes.func.isRequired,
    onClickSubmit: PropTypes.func.isRequired,
    onSubmit: PropTypes.func.isRequired,
    onUmount: PropTypes.func.isRequired,
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object
};
import {connect} from 'react-redux';
import {
    getDetail,
    clearData,
    tableSearch,
    onClickClose,
    onClickSubmit,
    onClickRelease
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);

const mapStateToProps = (state, ownProps) => ({
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']) || state.getIn(['page', 'domainData', 'initData', 'isFetching']),
    detail: getDetailData(state),
    permissions: getPermissions(state),
});

const mapDispatchToProps = (dispatch, ownProps) => ({
    init: () => dispatch(getDetail(ownProps.id)),
    onCancel: () => {
        ownProps.history.push(routes.query.url());
    },
    onClickClose: (id, data) =>
        dispatch(onClickClose(id, data)).then(success => {
            if(success) {
                dispatch(tableSearch({
                    pageIndex: PAGE.index
                }));
                ownProps.history.push(routes.query.url());
            }
            return success;
        }),
    onClickSubmit: (id, data) =>
        dispatch(onClickSubmit(id, data)).then(success => {
            if(success) {
                dispatch(tableSearch({
                    pageIndex: PAGE.index
                }));
                ownProps.history.push(routes.query.url());
            }
            return success;
        }),
    onClickRelease: (id, data) =>
        dispatch(onClickRelease(id, data)).then(success => {
            if(success) {
                dispatch(tableSearch({
                    pageIndex: PAGE.index
                }));
                ownProps.history.push(routes.query.url());
            }
            return success;
        }),
    onUmount: () => dispatch(clearData()),
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(Detail));
